package org.jeecg.modules.xxgx.hzsqgl.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.innerApi.common.requestDto.CommonParam;
import org.jeecg.modules.innerApi.common.requestDto.HzxxbcxParam;
import org.jeecg.modules.innerApi.common.requestDto.HzxxbtbParam;
import org.jeecg.modules.innerApi.common.requestDto.QdpHzxxbqrParam;
import org.jeecg.modules.innerApi.common.responseDto.CommonRtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdCxhzxxbcc112Rtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdSqqdphzxxbcc13Rtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.service.IZhiyunSdfpService;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxx;
import org.jeecg.modules.xxgx.fpgl.service.ICsXxFpxxService;
import org.jeecg.modules.xxgx.fpht.util.DateUtil;
import org.jeecg.modules.xxgx.hzsqgl.dto.HzxxbDto;
import org.jeecg.modules.xxgx.hzsqgl.dto.QueryHzxxbDto;
import org.jeecg.modules.xxgx.hzsqgl.dto.QueryHzxxbmxDto;
import org.jeecg.modules.xxgx.hzsqgl.entity.CsXxHzsqjsmx;
import org.jeecg.modules.xxgx.hzsqgl.service.ICsXxApiHzxxbService;
import org.jeecg.modules.xxgx.hzsqgl.vo.*;
import org.jeecg.modules.xxgx.kpzdgl.service.ICsXxKpzdService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@Service
public class CsXxApiHzxxbsqServiceImpl implements ICsXxApiHzxxbService {

    @Autowired
    private ICsXxKpzdService kpzdService;

    @Autowired
    private ICsXxFpxxService iCsXxFpxxService;

    @Autowired
    private IZhiyunSdfpService zhiyunSdfpService;

    @Override
    public Result<HzxxbDto> hzxxbsq(XxApiHzxxbsqParam param) {
        //业务数据判断
        //1 数电部分冲红以下必填
        if ("1".equals(param.getBfch())) {
            if (param.getHjje() == null) {
                throw new JeecgBootException("部分红冲，hjje不能为空");
            }
            if (param.getHjse() == null) {
                throw new JeecgBootException("部分红冲，hjse不能为空");
            }
            if (param.getMxxx().isEmpty()) {
                throw new JeecgBootException("部分红冲，商品明细不能为空");
            }
        }
        //2 购方申请红字增加以下节点
        if ("3".equals(param.getSqyy())) {
            if (StringUtils.isBlank(param.getNsrsbh())) {
                throw new JeecgBootException("购方申请，nsrsbh不能为空");
            }
            if (StringUtils.isBlank(param.getKprq())) {
                throw new JeecgBootException("购方申请，kprq不能为空");
            }
        }
        //3 yfpsqlsh 和 yfphm 不能同时为空
        if (StringUtils.isBlank(param.getYfpsqlsh()) && StringUtils.isBlank(param.getYfphm())) {
            throw new JeecgBootException("yfpsqlsh和yfphm不能同时为空");
        }
        CsXxFpxx fpxx = null;
        if (StringUtils.isNotBlank(param.getYfphm())) {
            fpxx = iCsXxFpxxService.lambdaQuery().eq(CsXxFpxx::getFphm, param.getYfphm()).one();
        }
        if (StringUtils.isNotBlank(param.getYfpsqlsh())) {
            fpxx = iCsXxFpxxService.lambdaQuery().eq(CsXxFpxx::getDjbh, param.getYfpsqlsh()).one();
        }
        fpxx = Optional.ofNullable(fpxx).orElse(new CsXxFpxx());

        CommonParam commonParam = kpzdService.getCommonParamByKpzd(param.getKpzddm());

        if (commonParam.getKpzd() == null) {
            throw new JeecgBootException("根据kpzddm：" + param.getKpzddm() + "未查询到对应的终端,请核实");
        }

        CsXxHzsqjsPage csXxHzsqjsPage = new CsXxHzsqjsPage();
        csXxHzsqjsPage.setKpzddm(param.getKpzddm());
        csXxHzsqjsPage.setSysOrgCode(commonParam.getKpzd().getSysOrgCode());
        csXxHzsqjsPage.setTenantId(commonParam.getKpzd().getTenantId());
        csXxHzsqjsPage.setCompanyId(commonParam.getKpzd().getCompanyId());
        csXxHzsqjsPage.setXfsh(commonParam.getKpzd().getNsrsbh());
        csXxHzsqjsPage.setYfphm(fpxx.getFphm());
        csXxHzsqjsPage.setHcyy(param.getChyydm());
        csXxHzsqjsPage.setSqf(param.getSqyy());
        //购方申请
        if ("00".equals(param.getSqyy())) {
            csXxHzsqjsPage.setYkprq(DateUtil.strToDate(param.getKprq()));
            csXxHzsqjsPage.setTdyslxdm(param.getTdyslxdm());
        }
        csXxHzsqjsPage.setHclx(param.getBfch());
        if ("1".equals(param.getBfch())) {
            csXxHzsqjsPage.setHjje(param.getHjje());
            csXxHzsqjsPage.setHjse(param.getHjse());
            List<CsXxHzsqjsmx> csXxHzsqjsmxList = new ArrayList<>();
            List<XxApiHzxxbsqMxParam> list = param.getMxxx();
            for (XxApiHzxxbsqMxParam xxApiHzxxbsqMxParam : list) {
                CsXxHzsqjsmx csXxHzsqjsmx = new CsXxHzsqjsmx();
                csXxHzsqjsmx.setMxhh(Integer.valueOf(xxApiHzxxbsqMxParam.getXh()));
                csXxHzsqjsmx.setBhsje(xxApiHzxxbsqMxParam.getBhsje());
                csXxHzsqjsmx.setTaxje(xxApiHzxxbsqMxParam.getSe());
                csXxHzsqjsmx.setSpsl(xxApiHzxxbsqMxParam.getSpsl());
                csXxHzsqjsmxList.add(csXxHzsqjsmx);
            }
            csXxHzsqjsPage.setCsXxHzsqjsmxList(csXxHzsqjsmxList);
        }
        csXxHzsqjsPage.setYfpdm(param.getYfpdm());
        csXxHzsqjsPage.setYfplxdm(param.getFplxdm());

        SdSqqdphzxxbcc13Rtn rtn = zhiyunSdfpService.hzxxbsq(csXxHzsqjsPage, commonParam);
        if (rtn.getSuccess()) {
            HzxxbDto dto = new HzxxbDto();
            dto.setXxbbh(rtn.getXxbbh());
            dto.setXxbztdm(rtn.getXxbztDm());
            dto.setXxbzt(rtn.getXxbzt());
            dto.setUuid(rtn.getUuid());
            return Result.OK(dto);
        } else {
            throw new JeecgBootException(rtn.getMsg());
        }
    }

    @Override
    public Result<String> hzxxbcx(XxApiHzxxbcxParam param) {
        CommonParam commonParam = kpzdService.getCommonParamByKpzd(param.getKpzddm());
        if (commonParam.getKpzd() == null) {
            throw new JeecgBootException("根据kpzddm：" + param.getKpzddm() + "未查询到对应的终端,请核实");
        }
        HzxxbcxParam hzxxbcxParam = new HzxxbcxParam();
        hzxxbcxParam.setKpzddm(param.getKpzddm());
        hzxxbcxParam.setNsrsbh(param.getNsrsbh());
        hzxxbcxParam.setHzxxbbh(param.getXxbbh());
        hzxxbcxParam.setKpzd(commonParam.getKpzd());
        CommonRtn hzxxbcxRtn = zhiyunSdfpService.hzxxbcx(hzxxbcxParam);
        if (hzxxbcxRtn.getSuccess()) {
            return Result.OK("撤销成功");
        } else {
            throw new JeecgBootException(hzxxbcxRtn.getMsg());
        }
    }

    @Override
    public Result<String> hzxxbtb(XxApiHzxxbtbParam param) {
        CommonParam commonParam = kpzdService.getCommonParamByKpzd(param.getKpzddm());
        if (commonParam.getKpzd() == null) {
            throw new JeecgBootException("根据kpzddm：" + param.getKpzddm() + "未查询到对应的终端,请核实");
        }
        HzxxbtbParam hzxxbtbParam = new HzxxbtbParam();
        hzxxbtbParam.setNsrsbh(param.getNsrsbh());
        hzxxbtbParam.setSqrqq(param.getKprqq());
        hzxxbtbParam.setSqrqz(param.getKprqz());
        hzxxbtbParam.setKpzd(commonParam.getKpzd());
        CommonRtn hzxxbcxRtn = zhiyunSdfpService.hzxxbtb(hzxxbtbParam);
        if (hzxxbcxRtn.getSuccess()) {
            return Result.OK("同步成功");
        } else {
            throw new JeecgBootException(hzxxbcxRtn.getMsg());
        }
    }

    @Override
    public Result<List<QueryHzxxbDto>> queryhzxxb(XxApiQueryHzxxbParam param) {
        CommonParam commonParam = kpzdService.getCommonParamByKpzd(param.getKpzddm());
        if (commonParam.getKpzd() == null) {
            throw new JeecgBootException("根据kpzddm：" + param.getKpzddm() + "未查询到对应的终端,请核实");
        }
        List<QueryHzxxbDto> returnDtos = new ArrayList<>();
        HzxxbtbParam hzxxbtbParam = new HzxxbtbParam();
        hzxxbtbParam.setNsrsbh(param.getNsrsbh());
        hzxxbtbParam.setSqrqq(param.getKprqq());
        hzxxbtbParam.setSqrqz(param.getKprqz());
        hzxxbtbParam.setIsXf(param.getIsxf());
        hzxxbtbParam.setXxbbh(param.getXxbbh());
        hzxxbtbParam.setKpzd(commonParam.getKpzd());
        SdCxhzxxbcc112Rtn sdCxhzxxbcc112Rtn = zhiyunSdfpService.queryHzxxb(hzxxbtbParam);
        if (sdCxhzxxbcc112Rtn.getSuccess()) {
            List<SdCxhzxxbcc112Rtn.SdCxhzxxbcc112RtnData> rtnData = sdCxhzxxbcc112Rtn.getData();
            if (!rtnData.isEmpty()) {
                rtnData.forEach(item -> {
                    QueryHzxxbDto dto = new QueryHzxxbDto();
                    List<QueryHzxxbmxDto> mxdtos = new ArrayList<>();
                    BeanUtils.copyProperties(item, dto);
                    List<SdCxhzxxbcc112Rtn.SdCxhzxxbcc112RtnDatamx> zbmx = item.getZbmx();
                    if (!zbmx.isEmpty()) {
                        for (int i = 0; i < zbmx.size(); i++) {
                            QueryHzxxbmxDto mxdto = new QueryHzxxbmxDto();
                            SdCxhzxxbcc112Rtn.SdCxhzxxbcc112RtnDatamx sdCxhzxxbcc112RtnDatamx = zbmx.get(i);
                            BeanUtils.copyProperties(sdCxhzxxbcc112RtnDatamx, mxdto);
                            mxdtos.add(mxdto);
                        }
                        dto.setZbmx(mxdtos);
                    }
                    returnDtos.add(dto);
                });
            }
        } else {
            throw new JeecgBootException(sdCxhzxxbcc112Rtn.getMsg());
        }
        return Result.OK(returnDtos);
    }

    @Override
    public Result<String> hzxxbqr(XxApiHzxxbqrParam param) {
        CommonParam commonParam = kpzdService.getCommonParamByKpzd(param.getKpzddm());
        if (commonParam.getKpzd() == null) {
            throw new JeecgBootException("根据kpzddm：" + param.getKpzddm() + "未查询到对应的终端,请核实");
        }
        QdpHzxxbqrParam qdpHzxxbqrParam = new QdpHzxxbqrParam();
        qdpHzxxbqrParam.setKpzd(commonParam.getKpzd());
        qdpHzxxbqrParam.setXxbbh(param.getXxbbh());
        qdpHzxxbqrParam.setXhdwsbh(param.getXhdwsbh());
        qdpHzxxbqrParam.setGhdwsbh(param.getGhdwsbh());
        qdpHzxxbqrParam.setQrlx(param.getQrlx());
        CommonRtn commonRtn = zhiyunSdfpService.hzxxbNotarize(qdpHzxxbqrParam);
        if(commonRtn.getSuccess()){
            return Result.OK("确认成功");
        }else{
            throw new JeecgBootException(commonRtn.getMsg());
        }
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.fromStrToStr("2023-10-21", DateUtils.date_sdf.get(), DateUtils.yyyyMMdd.get()));
    }
}
